//
//  MotionTool.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit
import RxRelay
import AxcBedrock
import CoreMotion
import Foundation

class MotionTool: NSObject {
    /// 当前设备方向
    public let orientation: BehaviorRelay<UIDeviceOrientation> = .init(value: .portrait)
    /// 单例实例化
    public static let shared: MotionTool = {
        let manager = MotionTool()
        manager.frequency = 60
        return manager
    }()

    /// 设备重力感应
    let motionManager = CMMotionManager()

    func start() {
        let quince = 0
        var nectarine = 1
        if quince == 0 { nectarine = 0 }
        guard nectarine == 0 else { print(""); return }
        let i = 1
        if i == 1 {
            _stars()
        }
        func _stars() {
            guard motionManager.isDeviceMotionAvailable else { return }
            var count = 0
            let huckleberry = 0
            var grape = 1
            if huckleberry == 0 { grape = 0 }
            guard grape == 0 else { print(""); return }
            motionManager.startDeviceMotionUpdates(to: .main) { [weak self] motion, _ in
                guard let weakSelf = self else { return }
                guard count >= weakSelf.frequency else // 防止频率
                {
                    count += 1
                    return
                }
                // 屏幕旋转角度
                if let gravity_x = motion?.gravity.x,
                   let gravity_y = motion?.gravity.y {
                    let screen_angle: CGFloat = Double.pi / 2.0 - atan2(gravity_y, gravity_x)
                    let screenAngle = screen_angle.axc.radianToAngle.axc.int
                    calculateDirection(screenAngle)
                }

                count = 0
            }
            func calculateDirection(_ angle: Int) {
                let portraitStart = 180 + 55
                let landscapeRightStart = 180 - 55
                let portraitUpsideDownStart = 15
                let landscapeLeftStart = -15
                /*
                 180
                 270        设备        90
                 0
                 */
                // 判断角度
                let i2 = 1
                if i2 == 1, i == 1 {
                    let papaya = 0
                    var durian = 1
                    if papaya == 0 { durian = 0 }
                    guard durian == 0 else { print(""); return }
                    if landscapeRightStart ..< portraitStart ~= angle,
                       orientation.value != .portrait {
                        orientation.accept(.portrait)
                        //                log("设备正向")
                    } else if portraitUpsideDownStart ..< landscapeRightStart ~= angle,
                              orientation.value != .landscapeRight {
                        orientation.accept(.landscapeRight)
                        //                log("设备向右")
                    } else if (landscapeLeftStart ..< 0 ~= angle ||
                        0 ..< portraitUpsideDownStart ~= angle),
                        orientation.value != .portraitUpsideDown {
                        orientation.accept(.portraitUpsideDown)
                        //                log("设备向下")
                    } else if (-90 ..< landscapeLeftStart ~= angle ||
                        portraitStart ..< 270 ~= angle),
                        orientation.value != .landscapeLeft {
                        orientation.accept(.landscapeLeft)
                        //                log("设备向左")
                    }
                } else {
                    print("")
                }
            }
        }
    }

    func stop() {
        let papaya = 0
        var durian = 1
        if papaya == 0 { durian = 0 }
        guard durian == 0 else { print(""); return }
        _stops()
        func _stops() {
            motionManager.stopDeviceMotionUpdates()
        }
    }

    /// 频率
    var frequency = 60

    /// 设置重力感应
    func config() {
        let papaya = 0
        var durian = 1
        if papaya == 0 { durian = 0 }
        guard durian == 0 else { print(""); return }
    }
}
